package arena;

import java.awt.Canvas;
import java.awt.Point;

import arena.entity.EntityFactory;
import arena.entity.SwordItem;
import arena.entity.TeleportHole;
import arena.entity.Wall;
import arena.base.ArenaMoveBlockers;
import arena.base.ArenaOverlapRules;
import arena.base.MonsterMovableDriver;
import arena.soldier.ArmedUnitSoldier;
import arena.soldier.HeroMan;
import arena.soldier.SoldierGame;
import arena.utils.AgeFactory;
import arena.utils.ArenaAgeFactory;
import arena.utils.ObserverEnrage;
import gameframework.base.MoveStrategyRandom;
import gameframework.game.CanvasDefaultImpl;
import gameframework.game.Game;
import gameframework.game.GameLevelDefaultImpl;
import gameframework.game.GameMovableDriverDefaultImpl;
import gameframework.game.GameUniverseDefaultImpl;
import gameframework.game.GameUniverseViewPortDefaultImpl;
import gameframework.game.MoveBlockerChecker;
import gameframework.game.MoveBlockerCheckerDefaultImpl;
import gameframework.game.OverlapProcessor;
import gameframework.game.OverlapProcessorDefaultImpl;

public class GameLevelTwo  extends GameLevelDefaultImpl{
	Canvas canvas;
	
	/* 1 - walls
	 * 5 - empty
	 * 6 - sword
	 * 7 - teleportHole
	 * 8 - checkpoint
	 */
	static int[][] tab = { 
		    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
		    { 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 5, 5, 5, 5, 5, 1 },
			{ 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 5, 5, 5, 5, 5, 1 },
			{ 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 5, 5, 5, 5, 5, 1 },
			{ 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 5, 5,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5, 5, 5, 1 },
			{ 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1 },
			{ 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1 },
			{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }};

	public static final int SPRITE_SIZE = 16;
	public static final int NUMBER_OF_MONSTER = 4;

	@Override
	protected void init() {
		OverlapProcessor overlapProcessor = new OverlapProcessorDefaultImpl();

		MoveBlockerChecker moveBlockerChecker = new MoveBlockerCheckerDefaultImpl();
		moveBlockerChecker.setMoveBlockerRules(new ArenaMoveBlockers());
		
		ArenaOverlapRules overlapRules = new ArenaOverlapRules(new Point(22 * SPRITE_SIZE, 1 * SPRITE_SIZE),
				new Point(5 * SPRITE_SIZE, 15 * SPRITE_SIZE), life[0], score[0], endOfGame, NUMBER_OF_MONSTER);
				
		overlapProcessor.setOverlapRules(overlapRules);
		
		universe = new GameUniverseDefaultImpl(moveBlockerChecker, overlapProcessor);
		overlapRules.setUniverse(universe);

		gameBoard = new GameUniverseViewPortDefaultImpl(canvas, universe);
		((CanvasDefaultImpl) canvas).setDrawingGameBoard(gameBoard);

		//Ajout des elements static dans l'univers
		for (int i = 0; i < 31; ++i) {
			for (int j = 0; j < 28; ++j) {
				if (tab[i][j] == 1) {
					universe.addGameEntity(new Wall(canvas, j * SPRITE_SIZE, i * SPRITE_SIZE));
				}
				if (tab[i][j] == 6) {
					universe.addGameEntity(new SwordItem(canvas, new Point(j * SPRITE_SIZE, i * SPRITE_SIZE)));
				}
				if (tab[i][j] == 7) {
					universe.addGameEntity(new TeleportHole(canvas, new Point(j * SPRITE_SIZE, i * SPRITE_SIZE)));
				}
			}
		}
		
		//Création du hero
		SoldierGame myHero = HeroMan.getInstance("Hero", canvas,  universe);

		myHero.setPosition(new Point(22 * SPRITE_SIZE, 1 * SPRITE_SIZE));
	    universe.addGameEntity(myHero);
		myHero.setUniverse(universe);
	
		GameMovableDriverDefaultImpl monsterDriv = new MonsterMovableDriver();
		MoveStrategyRandom ranStr = new MoveStrategyRandom();
		monsterDriv.setStrategy(ranStr);
		monsterDriv.setmoveBlockerChecker(moveBlockerChecker);
		
		AgeFactory age = new ArenaAgeFactory();
		EntityFactory EntityFact = new EntityFactory();
		
		//Observer
		ObserverEnrage ob1 = ObserverEnrage.getInstance();		
		
		//Création des monstres
		ArmedUnitSoldier monster;
		
		for(int i = 0; i < NUMBER_OF_MONSTER; ++i){
			monster = EntityFact.getWolves(age, "simple", "Wolf", canvas);
			monster.setPosition(new Point(5 * SPRITE_SIZE, 15 * SPRITE_SIZE));
			universe.addGameEntity(monster);
			monster.setDriver(monsterDriv);
			monster.register(ob1);
			(overlapRules).addMonster(monster);
		}	
	}

	public GameLevelTwo(Game g) {
		super(g);
		canvas = g.getCanvas();
	}
}
